// pages/order/order-list.js
const app = getApp()
const pool = new Map()
Page({

    data: {
        status: null
    },

    onLoad: function(options) {
        this.setData(options)
        let tabs = app.tabs
        let statusObj = {}
        Object.keys(tabs).forEach(k => {
            statusObj[tabs[k].status] = tabs[k].name
        })
        Object.assign(statusObj, {
            '70': '待退款',
            '71': '退款完成',
        })
        this.setData({
            statusObj,
            tabs
        })
        this.loadData()
    },

    clickTab(evt) {
        const status = evt.currentTarget.dataset.status
        if (status != this.data.status) {
            this.setData({
                status
            })
            let tab = pool.get(this.data.status)
            if (tab && tab.list.length == 0) {
                tab.page = 1
                tab.nextPage = false
            }
            this.loadData()
        }
    },
    //order_source 1社区 2站点 3超市
    loadData() {
        let tab = pool.get(this.data.status)
        if (!tab) {
            tab = {
                page: 1,
                nextPage: true,
                list: []
            }
            pool.set(this.data.status, tab)
        }
        this.setData(tab)
        if (!tab.nextPage) {
            this.setData(tab)
            return
        }

        if (tab.page == 1) {
            wx.showLoading({
                title: '加载中...',
            })
        }

        if (this.data.loading) {
            return
        }

        this.setData({
            loading: true
        })

        app.http.get("order/list?page=" + tab.page + '&status=' + this.data.status).then(res => {
            tab.list.push(...res.data.list)
            tab.page++
                tab.nextPage = res.data.nextPage
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
            this.setData({
                loading: false
            })
        }).catch(err => {
            app.wxToast.error(err.message)
            this.setData({
                loading: false
            })
        })
    },
    onPullDownRefresh: function() {
        let tab = pool.get(this.data.status)
        tab.page = 1
        tab.nextPage = true
        tab.list = []
        this.loadData()
    },

    onReachBottom: function() {
        this.loadData()
    },

})